<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .dragger {
            width: 100px;
            height: 100px;
            background-color: yellow;
            cursor: pointer;
            position: absolute;
        }
    </style>
</head>

<body>
    <div class="dragger" id="dragger1">

    </div>

</body>
<script>
    var id = "#dragger1"
    class Dragger {
        constructor(id) {
            this.box = document.querySelector(id)
            this.bindEvent()
        }
        bindEvent() {
            this.box.onmousedown = e => {
                var e = e || window.event
                var x = e.offsetX
                var y = e.offsetY
                document.onmousemove = e => {
                    this.box.style.left = e.clientX - x + "px"
                    this.box.style.top = e.clientY - y + "px"
                }
                document.onmouseup = () => {
                    document.onmousemove = null
                    this.box.onmouseup = null
                }
            }
        }
    }
    new Dragger(id)
</script>

</html>